package cn.juque.jdfsystem.controller.message;

import cn.hutool.core.bean.BeanUtil;
import cn.juque.common.base.BaseRequestVO;
import cn.juque.common.base.BaseResponseVO;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.disruptor.DisruptorHandler;
import cn.juque.jdfsystem.controller.BaseController;
import cn.juque.jdfsystem.disruptor.bo.FileAddBodyBO;
import cn.juque.jdfsystem.disruptor.bo.FileChangeBodyBO;
import cn.juque.jdfsystem.disruptor.bo.FileDeleteBodyBO;
import cn.juque.jdfsystem.disruptor.event.FileAddDisruptorEventServiceImpl;
import cn.juque.jdfsystem.disruptor.event.FileChangeDisruptorEventServiceImpl;
import cn.juque.jdfsystem.disruptor.event.FileDeleteDisruptorEventServiceImpl;
import cn.juque.jdfsystem.vo.message.FileAddParamVO;
import cn.juque.jdfsystem.vo.message.FileChangeParamVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>FileMessageController</li>
 * </ul>
 * @date 2023-01-11 18:05:44
 **/
@Slf4j
@Controller
@RequestMapping("/jdf/message/file")
public class FileMessageController extends BaseController {

    @Resource
    private DisruptorHandler disruptorHandler;

    /**
     * 同步变更状态
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public BaseResponseVO<Boolean> add(@RequestBody BaseRequestVO<FileAddParamVO> request) {
        this.validatorUtil.fastValidate(request);
        FileAddParamVO fileChangeParamVO = request.getParam();
        FileAddBodyBO bodyBO = new FileAddBodyBO();
        BeanUtil.copyProperties(fileChangeParamVO, bodyBO);
        bodyBO.setBusinessEventService(FileAddDisruptorEventServiceImpl.BEAN);
        this.disruptorHandler.publish(bodyBO);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 同步变更状态
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/changeFlag")
    @ResponseBody
    public BaseResponseVO<Boolean> changeFlag(@RequestBody BaseRequestVO<FileChangeParamVO> request) {
        this.validatorUtil.fastValidate(request);
        FileChangeParamVO fileChangeParamVO = request.getParam();
        FileChangeBodyBO bodyBO = new FileChangeBodyBO();
        BeanUtil.copyProperties(fileChangeParamVO, bodyBO);
        bodyBO.setBusinessEventService(FileChangeDisruptorEventServiceImpl.BEAN);
        this.disruptorHandler.publish(bodyBO);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }

    /**
     * 同步变更状态
     *
     * @param request BaseRequestVO
     * @return BaseResponseVO
     */
    @PostMapping(value = "/delete")
    @ResponseBody
    public BaseResponseVO<Boolean> delete(@RequestBody BaseRequestVO<List<String>> request) {
        List<String> list = super.validatorUtil.checkNotNull(request);
        FileDeleteBodyBO bodyBO = new FileDeleteBodyBO();
        bodyBO.setAbsolutePathList(list);
        bodyBO.setBusinessEventService(FileDeleteDisruptorEventServiceImpl.BEAN);
        this.disruptorHandler.publish(bodyBO);
        return new BaseResponseVO<>(MessageEnum.OK, true);
    }
}
